## ----setup, include=TRUE, results='hide', message = FALSE, warning = FALSE, cache=FALSE--------------------------------------------
options(
  width = 70,
  stringsAsFactors = FALSE,
  digits = 2)


## ----------------------------------------------------------------------------------------------------------------------------------
TRUE ## logical
FALSE ## logical


## ----------------------------------------------------------------------------------------------------------------------------------
42L ## integer

1.5 ## double numeric

2+3i ## complex number


## ----------------------------------------------------------------------------------------------------------------------------------
"a" ## character


## ----------------------------------------------------------------------------------------------------------------------------------
NA ## logical
NA_integer_ ## integer
NA_real_ ## double / numeric
NA_character_ ## character
NA_complex_ ## complex


## ----------------------------------------------------------------------------------------------------------------------------------
factor(1:3)
factor(c("alice", "bob", "charlie"))
factor(letters[1:3])


## ----------------------------------------------------------------------------------------------------------------------------------
## vector
c(1, 2, 3)


## ----------------------------------------------------------------------------------------------------------------------------------
## scalar is just a vector of length one
c(1)
## matrix is a vector with dimensions
matrix(c(1:6), nrow = 3, ncol = 2)


## ----------------------------------------------------------------------------------------------------------------------------------
## vectors and matrices can only have one type of data (e.g., integer, logical, etc.)

## list is a vector of objects
## lists can have different type of objects in each element
list(
  c("a"),
  c(1, 2, 3),
  matrix(c(1:6), nrow = 3, ncol = 2)
  )


## ----------------------------------------------------------------------------------------------------------------------------------
## data frames are special type of lists
## where each element of the list is identical in length
data.frame(
  1:3,
  4:6)

## using non equal length objects causes problems
data.frame(
  1:3,
  4:5)

data.frame( 1:3, letters[1:3])


## ----------------------------------------------------------------------------------------------------------------------------------
##if not yet installed, run the below line of code if needed.
#install.packages("data.table")
library(data.table)
dataTable <- data.table( 1:3, 4:6)
dataTable
is.data.frame(dataTable)


## ----------------------------------------------------------------------------------------------------------------------------------
x <- 5
y = 3
x
y
is.integer(x)
is.double(y)
is.vector(x)


## ----------------------------------------------------------------------------------------------------------------------------------
x <- c("a", "b", "c")
x[1]
is.vector(x)
is.vector(x[1])
is.character(x[1])


## ----------------------------------------------------------------------------------------------------------------------------------
passingLetterGrades <- c("A", "B", "C")
passingLetterGrades[2]


## ----------------------------------------------------------------------------------------------------------------------------------
x2 <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 3, ncol = 2)
x2 ## print to see full matrix
x2[1, 2] ## row 1, column 2
x2[1, ] ## all row 1
x2[, 1] ## all column 1

## can also grab several at once
x2[c(1, 2), ] ## rows 1 and 2
x2[c(1, 3), ] ## rows 1 and 3

## can drop one element using negative values
x[-2] ## drop element two
x2[, -2] ## drop column two
x2[-1, ] ## drop row 1


is.vector(x2)
is.matrix(x2)


## ----------------------------------------------------------------------------------------------------------------------------------
## for lists using a single bracket
## returns a list with one element
y <- list(
  c("a"),
  c(1:3))

y[1]
is.vector(y[1])
is.list(y[1])
is.character(y[1])


## ----------------------------------------------------------------------------------------------------------------------------------
## using double bracket returns the object within that
## element of the list, nothing more
y[[1]]
is.vector(y[[1]])
is.list(y[[1]])
is.character(y[[1]])


## ----------------------------------------------------------------------------------------------------------------------------------
## can chain brackets togeter
y[[2]][3] ## second element of the list, third element of the vector


## ----------------------------------------------------------------------------------------------------------------------------------
x3 <- data.frame(
  A = 1:3,
  B = 4:6)

y2 <- list(
  C = c("a"),
  D = c(1, 2, 3))

x3$A
y2$C

## these are equivalent to
x3[["A"]]
y2[["C"]]


## ----------------------------------------------------------------------------------------------------------------------------------
is.list(x3)
is.list(y2)
is.matrix(x3)
is.matrix(y2)


## ----------------------------------------------------------------------------------------------------------------------------------
x3[1, 1]
x3[1, ]
x3[, 1]


## ----------------------------------------------------------------------------------------------------------------------------------
x3[1, "A"]
x3[, "A"]


## ----------------------------------------------------------------------------------------------------------------------------------
rownames(x3) <- c("first", "second", "third")

x3["second", "B"]


## ----------------------------------------------------------------------------------------------------------------------------------
x4 <- data.table(
  A = 1:3,
  B = 4:6)

x4[1, ]
x4[, A] #no quote needed for a column in data.table
x4[1, A]

x4[1:2, .(A, B)]

x4[1, "A", with = FALSE]


## ----------------------------------------------------------------------------------------------------------------------------------
`[`(x, 1)
`[`(x3, "second", "A")
`[[`(y, 2)


## ----------------------------------------------------------------------------------------------------------------------------------
NA == NA ## does not work
is.na(NA) ## works


## ----------------------------------------------------------------------------------------------------------------------------------
inherits(x3, "data.frame")
inherits(x2, "matrix")


## ----------------------------------------------------------------------------------------------------------------------------------
as.integer(3.8)
as.character(3)
as.numeric(3)
as.complex(3)
as.factor(3)
as.matrix(3)
as.data.frame(3)
as.list(3)

as.logical("a") ## NA no warning
as.logical(3) ## TRUE, no warning
as.numeric("a") ## NA with a warning


## ----------------------------------------------------------------------------------------------------------------------------------
####################MATH################################
###### Comparisons and logicals
4 > 4
4 >= 4
4 < 4
4 <= 4
4 == 4
4 != 4


## ----------------------------------------------------------------------------------------------------------------------------------
all.equal(1, 1.00000002, tolerance = .00001)


## ----------------------------------------------------------------------------------------------------------------------------------
TRUE | FALSE
FALSE | TRUE
TRUE & TRUE
TRUE & FALSE


## ----------------------------------------------------------------------------------------------------------------------------------
1:3 >= 3:1
c(TRUE, TRUE) | c(TRUE, FALSE)
c(TRUE, TRUE) & c(TRUE, FALSE)


## ----------------------------------------------------------------------------------------------------------------------------------
## for cases where you only want a single response
## such as for if else flow control
## can use && or ||, which stop evaluating after they confirm what it is
## for example
W
TRUE | W
## BUT
TRUE || W
W || TRUE
FALSE & W
FALSE && W


## ----------------------------------------------------------------------------------------------------------------------------------
c(TRUE, TRUE) || c(TRUE, FALSE)
c(TRUE, TRUE) && c(TRUE, FALSE)


## ----------------------------------------------------------------------------------------------------------------------------------
## two additional useful functions are
any(c(TRUE, FALSE, FALSE))
all(c(TRUE, FALSE, TRUE))
all(c(TRUE, TRUE, TRUE))


## ----------------------------------------------------------------------------------------------------------------------------------
3 + 3
3 - 3
3 * 3
3 / 3
(-27) ^ (1/3)
4 %/% .7
4 %% .3


## ----------------------------------------------------------------------------------------------------------------------------------
sqrt(3)
abs(-3)
exp(1)
log(2.71)


## ----------------------------------------------------------------------------------------------------------------------------------
cos(3.1415) ## cosine
?Trig


## ----------------------------------------------------------------------------------------------------------------------------------
x2
x2 * 3
x2 + 3
x2 %*% matrix(c(1, 1), 2)


## ----------------------------------------------------------------------------------------------------------------------------------
## transpose
t(x2)
## cross product
t(x2) %*% x2
## easier cross product
crossprod(x2)

## transpose cross product
x2 %*% t(x2)
## easier transpose cross product
tcrossprod(x2)

